package com.jaynux.app;

import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

public class PlayerNumActivity extends Activity implements RadioGroup.OnCheckedChangeListener {

	private RadioGroup radio1; //player number
	private RadioGroup radio2; //경기장 거리
	
	private Button nextBtn;
	
	private int playerNum = 4; //초기값
	private int meter = 10000; //초기값
	
	private Intent intent;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.playernum);
		intent = new Intent(this, SettingActivity.class);
		
		nextBtn = (Button)findViewById(R.id.playerNextBtn);
		nextBtn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final View dialog = View.inflate(getApplicationContext(), R.layout.playername, null); 
				final AlertDialog nameAD = new AlertDialog.Builder(PlayerNumActivity.this).setPositiveButton("ok", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface arg0, int arg1) {
						// TODO Auto-generated method stub
						//player name을 입력받는 변수
						String player1 = ((EditText)dialog.findViewById(R.id.player1Edit)).getText().toString();
						String player2 = ((EditText)dialog.findViewById(R.id.player2Edit)).getText().toString();
						String player3 = ((EditText)dialog.findViewById(R.id.player3Edit)).getText().toString();
						String player4 = ((EditText)dialog.findViewById(R.id.player4Edit)).getText().toString();
					
						intent.putExtra("playerNum", playerNum);
						intent.putExtra("meter", meter);
						intent.putExtra("player1", player1);
						intent.putExtra("player2", player2);
						intent.putExtra("player3", player3);
						intent.putExtra("player4", player4);
						startActivity(intent);
						finish();
					}
                	
                }).setView(dialog).setTitle("Player Name").create();         
               //plyer name을 입력하는 Linear Layout
				ViewGroup player2 = (ViewGroup)dialog.findViewById(R.id.player2);
                ViewGroup player3 = (ViewGroup)dialog.findViewById(R.id.player3);
                ViewGroup player4 = (ViewGroup)dialog.findViewById(R.id.player4);				
                
                //player number에 따라 입력하는 editview의 개수를 조절
                switch(playerNum) {
				case 3:
					player4.setVisibility(View.INVISIBLE);
					break;
				case 2:
					player3.setVisibility(View.INVISIBLE);
					player4.setVisibility(View.INVISIBLE);
					break;
				case 1:
					player2.setVisibility(View.INVISIBLE);
					player3.setVisibility(View.INVISIBLE);
					player4.setVisibility(View.INVISIBLE);
					break;
				}
                nameAD.show();
			}
		});
		
		radio1 = (RadioGroup)findViewById(R.id.radiogroup1);
		radio1.setOnCheckedChangeListener(this);
		
		radio2 = (RadioGroup)findViewById(R.id.radiogroup2);
		radio2.setOnCheckedChangeListener(this);
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent i = new Intent(PlayerNumActivity.this, RaceStartActivity.class);
        	startActivity(i);
        	finish();
			return true;
		} else 
			return false;
	}
	
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		
		if(radio1.equals(group)) {
			switch(checkedId) {
			case R.id.onebtn :
				playerNum = 1;
				break;
			case R.id.twobtn :
				playerNum = 2;
				break;
			case R.id.threebtn :
				playerNum = 3;
				break;
			case R.id.fourbtn :
				playerNum = 4;
				break;
			}
		} else {
			switch(checkedId) {
			case R.id.meter1btn :
				meter = 10000;
				break;
			case R.id.meter2btn :
				meter = 15000;
				break;
			case R.id.meter3btn :
				meter = 20000;
				break;
			case R.id.meter4btn :
				meter = 25000;
				break;
			}
		}
	}
}
